var update_vm = new Vue({
    el: 'main',
    data: {
        formData: {
            old_password: null,
            password: null,
            password_confirmation: null
        },
        hasError:false,
        show_old: false,
        show_new: false,
        show_confirm: false,
        tips:null
    },
    computed: {
        token() {
            return localStorage.token || Cookies.get('token')
        }
    },
    methods: {
        toggleInput(index) {
            switch (index) {
                case 0:
                    this.show_old = !this.show_old;
                    break;
                case 1:
                    this.show_new = !this.show_new;
                    break;
                case 2:
                    this.show_confirm = !this.show_confirm;
                    break;
                default:
                    break;
            }
        },
        validatePass(){
             var passExp = /^[a-zA-Z0-9]{6,20}$/
             if(!passExp.test(this.formData.old_password)){
                alert('请输入正确的旧密码！')
                return false
             }else if(!passExp.test(this.formData.password)){
                alert('请输入符合要求的密码！')
                return false
             }else if(this.formData.password !== this.formData.password_confirmation){
                alert('两次输入的密码不一致，请检查！')
                return false
             }else{
                return true
             }
        },
        updatePass() {
            if(!this.validatePass()){
                this.hasError = true
                return
            }else{
                this.hasError = false
            }
            var target = event.currentTarget
            $(target).button('loading');
            var self = this
            axios.put(window.config.host + '/user/reset_password', this.formData, {
                    headers: {
                        Authorization: 'Bearer ' + this.token
                    }
                })
                .then(function(res) {
                    if (res.status == 200) {
                        if(res.data.data.success){
                            alert('更改密码成功，请重新登录')
                            localStorage.userinfo = null
                            localStorage.token = null
                            Cookies.set('userinfo', null)
                            Cookies.set('token', null)
                            self.hasError = false
                            window.location.href = '/'
                        }else{
                            self.hasError = true
                            self.tips = res.data.data.message
                            alert(res.data.data.message)
                        }
                    } else {
                        alert(res.data.message)
                    }
                })
                .catch(function(err) {

                })
                .then(function() {
                    $(target).button('reset');
                })
        }
    }
})